package com.qifan.energy.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.qifan.energy.activity.WebsiteActivity;
import com.qifan.energy.global.Keys;
import com.qifan.energy.R;

import java.io.Serializable;

/**
 * Intent跳转封装工具类
 * 静态方法
 */
public class IntentUtils {

    /** 通过Class跳转界面 **/
    public static void startActivity(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        context.startActivity(intent);
    }

    public static void startActivityWithFlag(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /** 含有Bundle通过Class跳转界面 **/
    public static void startActivity(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    /** 自定传参的Intent通过Class跳转界面 **/
    public static void startActivity(Context context, Class<?> cls,@NonNull Intent intent) {
        intent.setClass(context, cls);
        context.startActivity(intent);
    }


    /** 带action的Activity跳转 **/
    public static void startActivity(Context context, String action, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.setAction(action);
        context.startActivity(intent);
    }

    /** 带Serializable参数调整**/
    public static void startActivity(Context context, String action, Serializable serializable, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.setAction(action);
        intent.putExtra(action, serializable);
        context.startActivity(intent);
    }

    /** 含有Bundle通过Action跳转界面 **/
    public static void startActivity(Context context, String action, Bundle bundle, Class<?> cls) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.setClass(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    /** 带action，startActivityForResult**/
    public static void startActivityForResult(Activity activity, Class<?> cls, String action, int requestCode) {
        Intent intent = new Intent(activity, cls);
        intent.setAction(action);
        activity.startActivityForResult(intent, requestCode);
    }

    /** 启动Service**/
    public static void startService(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        context.startService(intent);
    }

    /** 关闭Service**/
    public static void stopService(Context context, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        context.stopService(intent);
    }

    /*
     * 先跳转拨号盘
     * */
    public static void dialPhone(Activity activity, String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        activity.startActivity(intent);
    }

    /*
     * 直接拨打
     * */
    public static void callPhone(Activity activity, String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        if (ActivityCompat.checkSelfPermission(UIUtils.getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ToastUtils.showShort(UIUtils.getString(R.string.refused_phone_permission_error));
            return;
        }
        activity.startActivity(intent);
        /*PermissionUtils.permission(PermissionConstants.PHONE)
                .callback(new PermissionUtils.SimpleCallback() {
                    @Override
                    public void onGranted() {

                    }

                    @Override
                    public void onDenied() {
                        ToastUtils.showShort(UIUtils.getString(R.string.refused_phone_permission_error));
                    }
                }).request();*/
    }

    /**
     * 打开网页界面
     *
     * @param url        URL 网址
     * @param title      标题文字
     * @param autoTitle  是否自动获取标题
     */
    public static void startWebsite(Context context,String url,String title,boolean autoTitle) {
        Bundle bundle = new Bundle();
        bundle.putString(Keys.URL, url);
        bundle.putString(Keys.TITLE, title);
        bundle.putBoolean(Keys.AUTO_TITLE, autoTitle);
        startActivity(context, WebsiteActivity.class,bundle);
    }

    /* 带返回结果的跳转 */
    public static void startActivityForResult(Activity _this, Class<? extends Activity> _class, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(_this, _class);
        _this.startActivityForResult(intent, requestCode);
        _this = null;
    }

    /* 带返回结果的跳转(添加正向传值) */
    public static void startActivityForResult(Activity _this, Class<? extends Activity> _class, Intent intent, int requestCode) {
        intent.setClass(_this, _class);
        _this.startActivityForResult(intent, requestCode);
        _this = null;
    }
}
